package com.zjut.an.webclass.manager;

import java.util.List;

import javax.annotation.Resource;

import com.zjut.an.admin.dao.AnAdmin;
import com.zjut.an.news.dao.AnNews;
import com.zjut.an.news.manager.AnNewsManager;
import com.zjut.an.role.dao.AnRole;
import com.zjut.an.role.manager.AnRoleManager;
import com.zjut.an.util.module.PageModule;
import com.zjut.an.util.system.Status;
import com.zjut.an.webclass.dao.AnWebclass;
import com.zjut.an.webclass.dao.AnWebclassDAO;


public class AnWebclassManager {

	@Resource(name = "AnWebclassDAO")
	private AnWebclassDAO dao;
	
	@Resource(name = "AnNewsManager")
	private AnNewsManager manager;
	
	@Resource(name = "AnRoleManager")
	private AnRoleManager rmanager;
	
	public void addData(AnWebclass obj) {	
		dao.save(obj);
	}
	
	public void updateData(AnWebclass obj) {
		dao.update(obj);
	}
	
	public void deleteData(AnWebclass obj) {
		List newslist = manager.getPubDatasByWcId(1, 100, null, null, obj).getObjects();
		manager.deleteDatas(newslist);
		dao.delete(obj);
	}

	public void deleteDatas(List<AnWebclass> objllegeList) {
		for (AnWebclass anWebclass : objllegeList) {			
			dao.delete(anWebclass);
		}
	}
	
	public PageModule getDataByInstance (int pageNumber,int pageSize, String sort, String order,AnWebclass obj) {
		return dao.findByInstance(pageNumber, pageSize, sort, order, obj);
	}
	
	public List getDatas() {
		return dao.findAll();
	}

	public AnWebclass getData(int objId) {
		return dao.findById(objId);
	}
	
	public List getChildrenDatas(int objId) {
		return dao.findByWcParent(objId);
	}
	
	public List getTopDatas() {
		return dao.findByWcParent(0);
	}
	
	public List getMenuTopDatas() {
		AnWebclass obj = new AnWebclass();
		obj.setWcParent(0);
		obj.setWcMenuview(Status.CLASSMENUVIEW_TOP.getStatus());
		return this.getDataByInstance(1,1000, "wcOrder", "desc", obj).getObjects();
	}
	
	public List getBottomTopDatas() {
		AnWebclass obj = new AnWebclass();
		obj.setWcParent(0);
		obj.setWcMenuview(Status.CLASSMENUVIEW_BOTTOM.getStatus());
		return this.getDataByInstance(1,1000, "wcOrder", "desc", obj).getObjects();
	}
	
	public boolean inAdminWcIds(AnWebclass wc, AnAdmin admin) {//判断指定栏目的上，中，下栏目是否存在于管理员的管理栏目范围内
		System.out.println("###"+wc.getWcName());
		AnRole role = rmanager.getData(admin.getRoId());
		AnWebclass wcparent = null ;
		if (wc!=null && wc.getWcParent()!=0) wcparent = this.getData(wc.getWcParent());
		if (Status.POWER_YES.getStatus().equals(role.getCheckpower())) {
			return true;
		} else if (wc==null || admin.getWcIds()==null) {
			return false;
		} else if ((","+admin.getWcIds()+",").contains(","+wc.getWcId()+",") || (wcparent!=null && (","+admin.getWcIds()+",").contains(","+wcparent.getWcId()+","))) {
			return true;
		} else {
			List clist = this.getChildrenDatas(wc.getWcId());
			for (int j=0; j<clist.size(); j++) {
				AnWebclass cwc = (AnWebclass)clist.get(j);
				if ((","+admin.getWcIds()+",").contains(","+cwc.getWcId()+",")) {
					return true;
				}
			}
		} 
		return false;		
	}
}
